package io.renren.modules.work.controller;

import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.ConfigConstant;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.work.entity.*;
import io.renren.modules.work.service.*;
import io.renren.vo.PicDTO;
import io.renren.vo.PicVo;
import io.renren.vo.ScConsumablesVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 子工程
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-03-24 23:31:29
 */
@RestController
@RequestMapping("work/scprojectdtl")
public class ScProjectdtlController  extends AbstractController {
    @Autowired
    private ScProjectService scProjectService;
    @Autowired
    private ScProjectdtlService scProjectdtlService;

    @Autowired
    private ScConsumablesService scConsumableService;

    @Autowired
    private ScGoodsService scGoodsService;

    @Autowired
    private ScFileService scFileService;

    @Autowired
    private ScExtrasService scExtrasService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = scProjectdtlService.queryPage(params);

        return R.ok().put("page", page);
    }




    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ScProjectdtlEntity scProjectdtl){
        SysUserEntity user = getUser();
        scProjectdtl.setCremanid(Integer.parseInt(user.getUserId().toString()));
        scProjectdtl.setCreman(user.getUsername());
        scProjectdtlService.saveProjectDtl(scProjectdtl);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody String params){
        return scProjectdtlService.updateProjectdtl(params);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @Transactional
    public R delete(@RequestBody String[] projectdtlids){

		scProjectdtlService.removeByIds(Arrays.asList(projectdtlids));

		//删除子工程对应的器材
        for (String projectdtlid : projectdtlids) {
            QueryWrapper<ScConsumablesEntity> scConsumablesEntityQueryWrapper = new QueryWrapper<>();
            scConsumablesEntityQueryWrapper.eq("projectdtlid",projectdtlid);
            scConsumableService.remove(scConsumablesEntityQueryWrapper);
        }

        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{projectdtlid}")
    public R info(@PathVariable("projectdtlid") String projectid){
        QueryWrapper<ScProjectdtlEntity> scProjectdtlEntityQueryWrapper = new QueryWrapper<>();
        scProjectdtlEntityQueryWrapper.eq("projectdtlid",projectid);
        ScProjectdtlEntity scProjectdtl = scProjectdtlService.getOne(scProjectdtlEntityQueryWrapper);
        QueryWrapper<ScConsumablesEntity> scConsumablesEntityQueryWrapper= new QueryWrapper<>();
        scConsumablesEntityQueryWrapper.eq("projectdtlid",projectid);
        List<ScConsumablesEntity> scConsumablesEntityList = scConsumableService.list(scConsumablesEntityQueryWrapper);
        ScProjectEntity scProjectEntity = scProjectService.getById(scProjectdtl.getProjectid());
        //查询间接费
        QueryWrapper<ScExtrasEntity> scExtrasEntityQueryWrapper = new QueryWrapper<>();
        scExtrasEntityQueryWrapper.eq("projectid",projectid);
        ScExtrasEntity scExtras = scExtrasService.getOne(scExtrasEntityQueryWrapper);

        ArrayList<ScConsumablesVo> scConsumablesVos = new ArrayList<>();
        if(scConsumablesEntityList!=null){
            for (ScConsumablesEntity scConsumablesEntity : scConsumablesEntityList) {
                ScConsumablesVo scConsumablesVo = new ScConsumablesVo();
                BeanUtil.copyProperties(scConsumablesEntity,scConsumablesVo);
                scConsumablesVos.add(scConsumablesVo);
            }
        }
        //获取维修前后图片
        PicDTO beforePicDTO = new PicDTO();
        beforePicDTO.setProjectdtlid(scProjectdtl.getProjectdtlid());
        beforePicDTO.setFileType(ConfigConstant.PIC_BEFORE);
        PicDTO afterPicDTO = new PicDTO();
        afterPicDTO.setProjectdtlid(scProjectdtl.getProjectdtlid());
        afterPicDTO.setFileType(ConfigConstant.PIC_AFTER);
        List<PicVo> beforePic = scFileService.getPic(beforePicDTO);
        List<PicVo> afterPic = scFileService.getPic(afterPicDTO);

        //成功返回结果,返回json对象
        Map<String, Object> result = new HashMap<>();
        result.put("projectId",scProjectdtl.getProjectid());
        result.put("projectname",scProjectEntity.getProjectname());
        result.put("projectdtlid",scProjectdtl.getProjectdtlid());
        result.put("projectdtlName",scProjectdtl.getProjectdtlname());
        result.put("handleState",scProjectdtl.getHandleState());
        result.put("qualityState",scProjectdtl.getQualityState());
        result.put("workhours",scProjectdtl.getWorkhours());
        result.put("manhourfee",scProjectdtl.getManhourfee());
        result.put("materialcost",scProjectdtl.getMaterialcost());
        result.put("specialfee",scProjectdtl.getSpecialfee());
        result.put("outsourcingFee",scProjectdtl.getOutsourcingFee());
        result.put("memo",scProjectdtl.getMemo());
        result.put("skill",scProjectdtl.getSkill());
        result.put("brokenstatus",scProjectdtl.getBrokenstatus());
        result.put("safe",scProjectdtl.getSafe());
        result.put("scConsumables",scConsumablesVos);
        result.put("beforePic",beforePic);
        result.put("afterPic",afterPic);
        result.put("billno",scProjectdtl.getBillno());
        result.put("purno",scProjectdtl.getPurno());
        result.put("othercost",scProjectdtl.getOthercost());
        result.put("extrasPrice",scExtras==null?0:scExtras.getPrice());
        return R.ok().put("data", JSON.toJSONString(result));
    }


    /**
     * 提交保存子工程表单
     */
    @RequestMapping("/commit")
    public R commitProject(@RequestBody String params){
        SysUserEntity user = getUser();

        return scProjectdtlService.commitProject(params,user);
    }


}
